Shader "Anjum/FX/FB_Explosion" {
	Properties {
		_ParticleTex ("Particle SubUV", 2D) = "white" {}
		
		_HeatErosionScale("Heat Erosion Scale", float) = 1
		_HeatContrast("Heat Contrast", float) = 1
		_HeatColor("Heat Color", COLOR) = (0.624, 0.456, 0.237, 1)
		
		_Temperature_MacroUV_U_Repeat("Temperature MacroUV U Repeat", float) = 1
		_Temperature_MacroUV_V_Repeat("Temperature MacroUV V Repeat", float) = 1
		_Temperature_MacroUV_PannerSpeed("Temperature MacroUV PannerSpeed", float) = 1
		_Temperature_MacroUV_Brightness("Temperature MacroUV Brightness", float) = 1
		_Temperature_Contrast("Temperature Contrast", float) = 1
		_Temperature_Color("Temperature Color", COLOR) = (16,2.6,0.8,1)
		_Temperature_GreyPack("Temperature Grey Pack", 2D) = "white" {}
		_Temperature_TilableSmoke("Temperature Tilable Smoke", 2D) = "white" {}
		
		_Density_MacroUV_U_Repeat("Density MacroUV U Repeat", float) = 1
		_Denisty_MacroUV_V_Repeat("Density MacroUV V Repeat", float) = 1
		_Density_MacroUV_PannerSpeed("Density MacroUV PannerSpeed", float) = 1
		_Density_MacroUV_Brightness("Density MacroUV Brightness", float) = 1
		_Density_Contrast("Density Contrast", float) = 1
		_Density_Color("Density Color", COLOR) = (0,0,0,1)
		
		_Opacity_Erosion_Attenuation("Opacity Erosion Attenuation", float) = 4
	}
	
	SubShader {
		Tags{"Queue" = "Transparent"}
		
		Pass{
			Cull Off
			ZWrite Off
			Blend SrcColor One
			
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			
			uniform sampler2D _ParticleTex;
			
			uniform float _HeatErosionScale;
			uniform float _HeatContrast;
			uniform float4 _HeatColor;
			
			uniform float _Temperature_MacroUV_U_Repeat;
			uniform float _Temperature_MacroUV_V_Repeat;
			uniform float _Temperature_MacroUV_PannerSpeed;
			uniform float _Temperature_MacroUV_Brightness;
			uniform float _Temperature_Contrast;
			uniform float4 _Temperature_Color;
			uniform sampler2D _Temperature_GreyPack;
			uniform sampler2D _Temperature_TilableSmoke;
			
			uniform float _Density_MacroUV_U_Repeat;
			uniform float _Density_MacroUV_V_Repeat;
			uniform float _Density_MacroUV_PannerSpeed;
			uniform float _Density_MacroUV_Brightness;
			uniform float _Density_Contrast;
			uniform float4 _Density_Color;
			
			uniform float _Opacity_Erosion_Attenuation;
			
			struct vertexInput{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
				float4 color : COLOR;
			};
			
			struct vertexOutput{
				float4 pos : SV_POSITION;
				float4 tex : TEXCOORD0;
				float4 color : COLOR;
			};
			
			vertexOutput vert(vertexInput input){
				vertexOutput output;
				
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				output.tex = input.texcoord;
				output.color = input.color;
				
				return output;
				
			}
			
			//Going to try to use the color module as a dynamic parameter
			//RED: Temperature Control
			//GREEN: Heat Control
			//BLUE: Edge Blur Control
			//ALPHA: Regular alpha
			
			float4 frag(vertexOutput input) : COLOR{
				//float4 outputColor = tex2D(_ParticleTex, input.tex.xy)*input.color*input.color.a;
				float RANDOMIZER = 1.0;
				
				//////////////////****HEAT****//////////////////
				float4 textureSample = tex2D(_ParticleTex, input.tex.xy);
				float4 HEAT = (1-textureSample.r)+textureSample.a;
				HEAT *= _HeatErosionScale;
				
				float DP_HeatControl = input.color.g*RANDOMIZER;
				
				HEAT -= clamp(clamp(1-DP_HeatControl,0,1),0,1);
				HEAT = pow(HEAT, _HeatContrast);
				
				HEAT *= _HeatColor;
				HEAT *= DP_HeatControl;
				
				//////////////////****TEMPERATURE****//////////////////
				float2 textureSample_GreyPackUV;
				textureSample_GreyPackUV.x = input.tex.x*_Temperature_MacroUV_U_Repeat;
				textureSample_GreyPackUV.y = input.tex.y*_Temperature_MacroUV_V_Repeat;
				float4 textureSample_GreyPack = tex2D(_Temperature_GreyPack, textureSample_GreyPackUV.xy);
				
				float2 textureSample_SmokeUV;
				textureSample_SmokeUV.x = input.tex.x*_Temperature_MacroUV_U_Repeat;
				float temperature_PannerSpeed = _Time*_Temperature_MacroUV_PannerSpeed;
				textureSample_SmokeUV.y = input.tex.y*_Temperature_MacroUV_V_Repeat+temperature_PannerSpeed;
				float4 textureSample_Smoke = tex2D(_Temperature_TilableSmoke, textureSample_SmokeUV);
				
				float temperatureControl = clamp(textureSample.g-(1-input.color.r),0,1);
				
				float4 TEMPERATURE_MacroUV = textureSample_GreyPack.r*textureSample_Smoke*_Temperature_MacroUV_Brightness*temperatureControl;
				TEMPERATURE_MacroUV += temperatureControl;
				
				float temperatureControl2 = input.color.r*RANDOMIZER;
				TEMPERATURE_MacroUV *= temperatureControl2;
				
				float TEMPERATURE = pow(TEMPERATURE_MacroUV, _Temperature_Contrast)*_Temperature_Color*textureSample.g;
				
				//////////////////****DENSITY****//////////////////
				float2 textureSample_DENSITY_GreyPackUV;
				textureSample_DENSITY_GreyPackUV.x = input.tex.x*_Density_MacroUV_U_Repeat;
				float density_PannerSpeed = _Time*_Density_MacroUV_PannerSpeed;
				textureSample_DENSITY_GreyPackUV.y = input.tex.y*_Density_MacroUV_V_Repeat+density_PannerSpeed;
				float4 textureSample_Density = tex2D(_Temperature_TilableSmoke, textureSample_DENSITY_GreyPackUV);
				
				float4 DENSITY_PANNER = textureSample_Density*_Density_MacroUV_Brightness;
				float DENSITY_CONTRAST = pow(textureSample.r, _Density_Contrast);
				float4 FINAL_DENSITY = DENSITY_PANNER+DENSITY_CONTRAST;
				FINAL_DENSITY *= _Density_Color;
				
				
				//////////////////****OPACITY****//////////////////
				float EDGE_CONTROL = lerp(textureSample.b, textureSample.a, input.color.b*RANDOMIZER);
				float EROSION = textureSample.r*_Opacity_Erosion_Attenuation*(1-input.color.a);
				float OPACITY = clamp(EDGE_CONTROL-EROSION,0,1)*input.color.a;
				
				
				//////////////////****OUTPUT****//////////////////
				//outputColor *= input.color*input.color.a;
				float4 outputColor = (TEMPERATURE+HEAT+FINAL_DENSITY)*OPACITY;
				return outputColor;
			}
			
			ENDCG
		}
	}
}

